package com.bookcode;


import com.bookcode.dao.PersonRepository;

import com.bookcode.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

        @Autowired
        private PersonRepository repository;
        public static void main (String[]args){
        SpringApplication.run(DemoApplication.class, args);
    }
        @Override
        public void run (String...args) throws Exception {
        repository.deleteAll();           // 辅助功能，先删除已有记录
        repository.save(new Person("Alice", "Smith"));      // 存储Person信息
        repository.save(new Person("Bob", "Smith"));          // 存储Person信息
        // 将所有Person信息输出
        System.out.println("Persons found with findAll():");
        System.out.println("-------------------------------");
        for (Person person : repository.findAll()) {
            System.out.println(person);
        }
        System.out.println();
        // 找firstName为'Alice'的 Person，并将其信息输出到控制台
        System.out.println("Person found with findByFirstName('Alice'):");
        System.out.println("--------------------------------");
        System.out.println(repository.findByFirstName("Alice"));
// 找lastName为'Smith'的 Person，并将其信息输出到控制台
        System.out.println("Persons found with findByLastName('Smith'):");
        System.out.println("--------------------------------");
        for (Person Person : repository.findByLastName("Smith")) {
            System.out.println(Person);
        }
    }

}
